<template>
  <div class="app-container">
    <el-table
      cell-click="updateAuthor"
      @row-click="onRowClick"
      :row-class-name="tableRowClassName"
      v-loading="listLoading"
      :data="article"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="95" prop="id">
      </el-table-column>
      <el-table-column label="Title" prop="title"> </el-table-column>
      <el-table-column label="Author" width="110" align="center" prop="author">
      </el-table-column>
      <el-table-column
        label="Pageviews"
        width="110"
        align="center"
        type="index"
        :index="indexMethod"
      >
      </el-table-column>
      <el-table-column
        type="index"
        class-name="status-col"
        label="Status"
        width="110"
        align="center"
      >
        <template scope="scope">
          <div class="sub-body">
            <button @click="rePeoplemessageCard(scope.row.id)">修改</button>
            <button @click="del(scope.row.id)">删除</button>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="date"
        label="Display_time"
        width="200"
      >
      </el-table-column>
      -->
    </el-table>
  </div>
</template>

<script>
import { getList } from "@/api/table";

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger",
      };
      return statusMap[status];
    },
  },
  computed: {},
  data() {
    return {
      list: null,
      listLoading: true,
      article: [],
      index: "",
      currentRowIndex: "",
    };
  },
  created() {
    this.fetchData();
    this.axios
      .get(
        "https://www.fastmock.site/mock/e95dac21145f7756a1c5d5095b4e6486/myblogAdmin/articleList"
      )
      .then((result) => {
        console.log(result.data.data);
        this.$store.commit("addArticle", result.data.data);
        this.article = result.data.data;
      });
  },
  methods: {
    rePeoplemessageCard(val) {
      console.log(val);
      this.$router.push({
        path: "/example/update",
        query: {
          id: val,
        },
      });
    },
    del(val) {
      this.axios
        .get(
          "https://www.fastmock.site/mock/e95dac21145f7756a1c5d5095b4e6486/myblogAdmin/delete",
          {
            id: val,
          }
        )
        .then((result) => {
          console.log(this.currentRowIndex);
          if (result.data.code == 200) {
            this.article.splice(this.currentRowIndex, 1);
          }
        });
    },

    fetchData() {
      this.listLoading = true;
      getList().then((response) => {
        this.list = response.data.items;
        this.listLoading = false;
      });
    },
    indexMethod(index) {
      // this.index=index;
      return index;
    },
    tableRowClassName({ row, rowIndex }) {
      row.row_index = rowIndex;
    },
    onRowClick(row, event, column) {
      this.currentRowIndex = row.row_index;
    },
  },
};
</script>

